package org.huel.backend.controller;

import org.huel.backend.domain.entity.AbsencePrediction;
import org.huel.backend.service.impl.PredictionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("prediction")
public class AbsencePredictionController {

    @Autowired
    private PredictionService absencePredictionService;

    /**
     * 获取前三名最可能缺勤的学生
     * @param requestData 请求数据
     * @return 响应结果
     */
    @PostMapping("/top-three")
    public Map<String, Object> getTopThreeAbsencePredictions(@RequestBody(required = false) Map<String, Object> requestData) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<AbsencePrediction> predictions = absencePredictionService.getTopThreeAbsencePredictions();
            result.put("success", true);
            result.put("data", predictions);
            result.put("message", "获取成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "获取失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 根据课程获取前三名最可能缺勤的学生
     * @param requestData 请求数据，包含courseName
     * @return 响应结果
     */
    @PostMapping("/top-three-by-course")
    public Map<String, Object> getTopThreeAbsencePredictionsByCourse(@RequestBody Map<String, Object> requestData) {
        Map<String, Object> result = new HashMap<>();
        try {
            String courseName = (String) requestData.get("courseName");
            if (courseName == null || courseName.trim().isEmpty()) {
                result.put("success", false);
                result.put("message", "课程名称不能为空");
                return result;
            }

            List<AbsencePrediction> predictions = absencePredictionService.getTopThreeAbsencePredictionsByCourse(courseName);
            result.put("success", true);
            result.put("data", predictions);
            result.put("message", "获取成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "获取失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 获取所有课程列表
     * @param requestData 请求数据
     * @return 响应结果
     */
    @PostMapping("/courses")
    public Map<String, Object> getAllCourses(@RequestBody(required = false) Map<String, Object> requestData) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<String> courses = absencePredictionService.getAllCourses();
            result.put("success", true);
            result.put("data", courses);
            result.put("message", "获取成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "获取失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 根据学生ID获取预测信息
     * @param requestData 请求数据，包含studentId
     * @return 响应结果
     */
    @PostMapping("/by-student")
    public Map<String, Object> getPredictionsByStudentId(@RequestBody Map<String, Object> requestData) {
        Map<String, Object> result = new HashMap<>();
        try {
            String studentId = (String) requestData.get("studentId");
            if (studentId == null || studentId.trim().isEmpty()) {
                result.put("success", false);
                result.put("message", "学生ID不能为空");
                return result;
            }

            List<AbsencePrediction> predictions = absencePredictionService.getPredictionsByStudentId(studentId);
            result.put("success", true);
            result.put("data", predictions);
            result.put("message", "获取成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "获取失败: " + e.getMessage());
        }
        return result;
    }



    /**
     * 批量更新预测数据
     * @param requestData 请求数据，包含predictions数组
     * @return 响应结果
     */
    @PostMapping("/batch-update")
    public Map<String, Object> batchUpdatePredictions(@RequestBody Map<String, Object> requestData) {
        Map<String, Object> result = new HashMap<>();
        try {
            @SuppressWarnings("unchecked")
            List<Map<String, Object>> predictionsData = (List<Map<String, Object>>) requestData.get("predictions");

            if (predictionsData == null || predictionsData.isEmpty()) {
                result.put("success", false);
                result.put("message", "预测数据不能为空");
                return result;
            }

            // 转换数据并批量更新
            // 这里可以添加批量更新的逻辑

            result.put("success", true);
            result.put("message", "批量更新成功");
            result.put("count", predictionsData.size());
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "批量更新失败: " + e.getMessage());
        }
        return result;
    }
}